
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html>
<head>
	<meta charset="UTF-8">
    <title>制定入库单</title>
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
   	<link rel="stylesheet" href="/css/stock/c/Warehousing_Application_Registration.css" />
   <style type="text/css">
    	
    </style>
</head>
<body style="padding: 15px 50px;background-color: #eeeeee;">
	<div class="panel-heading hhead">
		<nav aria-label="breadcrumb">
		  <ol class="breadcrumb">
		  	<span style="color: blue;">您正在做的业务是：</span>
		    <li class="breadcrumb-item"><a href="#">库存管理</a></li>
		    <li class="breadcrumb-item"><a href="#">出入库调度管理</a></li>
		    <li class="breadcrumb-item active" aria-current="page">制定入库单</li>
		  </ol>
		</nav>
	</div>
	<form id="searchForm">
		<input type="hidden" name="sgatherId" id="sgatherId" value="">
		<input type="hidden" name="register" id="register2" value="">
		<input type="hidden" name="registerTime" id="registerTime2" value="">
	</form>
	<div style="margin-bottom: 2px">
		<div style="display: inline;margin-left: 95%;">
			<button class="btn btn-primary" id="back">返回</button>
		</div>
	</div> <!-- 此div改用栅格来做 -->
    <div id="designListBox">
        <div style="text-align: center">
            <h3>入库调度单</h3>
        </div>
        <div style="padding: 30px">
           <table class="tbl1">
               <tr>
                   <td class="col1">入库单编号：</td>
                   <td class="col2" id="gatherId"></td>
               </tr>
               <tr>
                   <td class="col1">入库理由：</td>
                   <td class="col2" id="reason"></td>
                   <td class="col1">入库详情理由：</td>
                   <td class="col2" id="reasonexact"></td>
               </tr>
           </table>

            <table class="table table-bordered" style="margin: 15px auto;">
                <thead>
                    <tr>
                        <th>序号</th><th>产品名称</th><th>产品编号</th>
                        <th>应入库件数</th><th>已入库件数</th>
                        <th>调度</th>
                    </tr>
                </thead>
                <tbody id="template">

                </tbody>
            </table>

            <table class="tbl3">
            	<tr>
                    <td class="col1">应入库件数：</td>
                    <td class="col2" id="Should"></td>
                    <td class="col1">已入库件数：</td>
					<td class="col2" id="already"></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div class="modal fade" id="proRegistraModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
		<div class="modal-dialog modal-xl">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
						</div>
						<div class="modal-body">
						    <div style="margin-bottom: 2px">

						    </div> <!-- 此div改用栅格来做 -->
						    <div id="designListBox2">
						        <div style="text-align: center">
						            <h3>入库调度单</h3>
						        </div>
						        <div style="padding: 30px">
						           <table class="tbl1">
						               <tr>
						                   <td class="col1">入库单编号：</td>
						                   <td class="col2" id="gatherId2"></td>
						                   <td class="col1">产品编号：</td>
						                   <td class="col2" id="productId"></td>
						               </tr>
						               <tr>
						                   <td class="col1">产品名称：</td>
						                   <td class="col2" id="productName"></td>
						                   <td class="col1">成本单价：</td>
						                   <td class="col2" id="costPrice"></td>
						               </tr>
						           </table>
						
						            <table class="table table-bordered" style="margin: 15px auto;">
						                <thead>
						                    <tr>
						                        <th>序号</th><th>库房名称</th><th>存储地址编号</th>
						                        <th>存储地址名称</th><th>存储单元简称</th><th>当前可存放数量</th>
						                        <th>本次入库数量</th>
						                    </tr>
						                </thead>
						                <tbody id="template2" style="display: none;">
											<tr>
												<td id="serialNum"></td>
												<td id="warehouseName"></td>
												<td id="areaId"></td>
												<td id="areaName"></td>
												<td id="referred"></td>
												<td id="amount"></td>
												<td class="num"><input class="num-input" value=""></td>
											</tr>
						                </tbody>
										<tbody id="warehouseTbl">

										</tbody>
						            </table>
						
						            <table class="tbl3">
						            	<tr>
						                    <td class="col1">登记人：</td>
						                    <td class="col2"><input type="text" class="txt_under_line" id="register"></td>
						                    <td class="col1">登记时间：</td> <td class="col2" id="registerTime"></td>
						                </tr>
						            	<tr>
						                    <td class="col1">应入库总件数：</td>
						                    <td class="col2" id="ShouldSum"></td>
						                    <td class="col1">已入库总件数：</td> <td class="col2" id="alreadySum"></td>
						                </tr>
						            </table>
						        </div>
						    </div>
    
						</div>
						<div class="modal-footer">
							<button id="submit" class="btn btn-danger" data-bs-dismiss="modal">
								确认<span class="glyphicon glyphicon-remove"></span>
							</button>
							<button id="btnConcel" class="btn btn-danger" data-bs-dismiss="modal">
								关闭<span class="glyphicon glyphicon-remove"></span>
							</button>
						</div>
					</div>
				</div>
	</div>
		
		<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
		<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
	<script type="text/javascript">
		$(function (){
			// 初始化数据
			ItemApprovalTable();

			$("#back").on("click",function (){
				window.location.href = "/s-gather/Formulat_Prepare_warehouse_entry";
			});

			$("#proRegistraModal").on("hidden.bs.modal", function () {
				$("#warehouseTbl").empty();
			})

			$("#submit").on("click",function (){

				//本次入库数量
				let num1 = parseInt($("#warehouseTbl .num-input").val());

				//应入库总数量
				let ShouldSum =parseInt($("#ShouldSum").text());
				let row = $("#warehouseTbl .num-input").parent().parent();
				//当前存储数量
				let storageNum = parseInt($(row).find("td:eq(5)").text());
				debugger;

				//获取出库编号
				let id=$("#sgatherId").val();
				//获取出库单编号
				let gatherId = $("#gatherId2").text();
				let rows = $("#warehouseTbl tr");
				let productId=$("#productId").text();
				let productDescribe = $("#productDescribe").text();
				let num = rows.find(".num-input").val();
				if(num==""){
					alert("请输入本次入库数量,才能提交");
					return;
				}
				let data = [];
				data.push({
					id:id,
					//入库详情理由
					productDescribe: productDescribe,
                    gatherId: gatherId,
					//产品编号
					productId: productId,
					//本次入库数量
					num:num
				})
				data=JSON.stringify(data);
				//调度
				$.ajax({
					url: "/s-gather/dispatch",
					type: "POST",
					data:  data,
					contentType: "application/json",
					dataType: "json",
					success: function (flag) {
						if (flag){
							window.location.href = "/s-gather/Prepare_warehouse_entry";
						}
					},
					error: function (e) {
					}
				})
			})
		})
		function ItemApprovalTable(){
			let id = sessionStorage.getItem("id");
			$("#id").val(id);
			$.ajax({
				url: "/s-gather/ItemApprovalTable?id="+id,
				type: "GET",
				dataType: "json",
				success: function (data) {
					console.log(data);
					if (data.errCode == "1") {
						alert(data.errMsg)
					} else {
						$("#gatherId").text(data.gatherId);
						$("#reason").text(data.reason);
						$("#reasonexact").text(data.reasonexact);
						$("#sgatherId").attr("value",data.id)
						$("#register2").attr("value",data.register)
						$("#registerTime2").attr("value",data.registerTime)
						// let a=$("#registerTime2").val()
						// alert(a);
						initialTemplate(data.id);
					}
				}
			})
		}
		function initialTotalSum(){
			// 初始化总和变量
			let totalOutbound = 0;
			let totalAlready = 0;
			// 选择表格中tbody下所有的tr元素，然后找到每个tr里面的第四列（索引为3，因为索引从0开始计数）
			$('#template tr').each(function () {
				let num = parseInt($(this).find('td').eq(3).text());
				let num2 = parseInt($(this).find('td').eq(4).text());
				totalOutbound += num;
				totalAlready += num2;
			});
			$("#Should").text(totalOutbound);
			$("#already").text(totalAlready);
		}
		function initialTemplate(parentId){
			$.ajax({
				url: "/s-gather/initialTemplate?id="+parentId,
				type: "GET",
				dataType: "json",
				success: function (data) {
					console.log(data);
					if (data.errCode == "1") {
						alert(data.errMsg)
					} else {
						let template = $("#template");
						// 清空表格
						template.empty();
						// 遍历数据并添加到表格中
						data.forEach(function (item, index) {
							let row = $("<tr>");
							row.append($("<td>").text(index + 1));
							row.append($("<td>").text(item.productName));
							row.append($("<td>").text(item.productId));
							row.append($("<td>").text(item.gatheredAmount));
							row.append($("<td>").text("0"));
							if(item.gatherTag =="2"){
								row.append($("<td>").text("完成"));
							}else{
								row.append($("<td><a href='javascript:Add("  + item.id+ ")' class='btn btn-outline-success dispatch' >调度</a><input type='hidden' name='id' value='"+item.id+"'></td>"))
							}
							template.append(row);
						})
						//初始化应出库总件数和已出库总件数
						initialTotalSum();
					}
				}
			})
		}
		function Add(id){
			// 存储模块id到sessionStorage
			$.ajax({
				url: "/s-gather-details/initialSGatherDetails?id=" + id,
				type: "get",
				datatype: "json",
				success: function (data) {
					// console.log(data)
					productId = data.productId;
					$('#productName').text(data.productName);
					$('#productId').text(data.productId);
					$("#costPrice").text(data.costPrice);
					$("#ShouldSum").text(data.gatheredAmount);
					initialArchiveTable(data.productId);
					//得到当前存储量
					$.ajax({
						url: "/s-gather/initialAmount",
						type: "POST",
						data: data.productId,
						contentType: "application/json",
						dataType: "json",
						success: function (data2) {
							console.log(data2);
							if(data2.sCell==null){
								alert("对不起，该产品未分配存储空间，请返回");
								$("#proRegistraModal").modal("hide");
							} else {
								$("#amount").text(data2.sCell.amount);
								$('#proRegistraModal').modal('show');
							}
						},
						error: function (e) {
							console.log(e);
						}
					})
				}
			})
			let gatherId = $("#gatherId").text();
			$("#gatherId2").text(gatherId);
			$("#register").val($("#register2").val());
			$("#registerTime").text($("#registerTime2").val());
			$("#alreadySum").text("0");
			return false;
		}
		//添加模态框中库房表数据
		function initialArchiveTable(productId){
			$.ajax({
				url: "/s-gather/initialStoreroomTable?id="+productId,
				type: "GET",
				contentType: "application/json",
				dataType: "json",
				success: function (data) {
					console.log(data);
					$("#warehouseTbl").empty();
					let template = $("#template2").children();

					let serialNum = 1;
					$.each(data,function (i,aa) {
						let tr = template.clone();
						$.each(aa,function (name,value) {
							if(name == "ssecondProductArea"){
								tr.find("#warehouseName").text(aa["ssecondProductArea"]["firstProductArea"]["swarehouseSetting"]["warehouseName"]);
								let firstId=aa["ssecondProductArea"]["firstProductArea"]["firstId"];
								let secondId=aa["ssecondProductArea"]["secondId"];
								let thirdId=aa["thirdId"];
								tr.find("#areaId").text(firstId + "-" + secondId + "-" +thirdId);

								let firstName = aa["ssecondProductArea"]["firstProductArea"]["firstName"];
								let secondName = aa["ssecondProductArea"]["secondName"];
								let thirdName = aa["thirdName"];
								tr.find("#areaName").text(firstName + "-" + secondName + "-" + thirdName);
								tr.find("#serialNum").text(serialNum++);
							}
						})
						$("#warehouseTbl").append(tr);
					})
				},
				error: function (e) {
				}
			})
		}
	</script>
</body>
</html>
